package demo.security.example.exception.handler;

import demo.security.example.exception.UserNotExistsException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * ControllerExceptionHandler
 *
 * @author lidj
 * @date 2018-01-09
 */
@ControllerAdvice
public class ControllerExceptionHandler {

    @ExceptionHandler(UserNotExistsException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Map<String,Object> handleUserNotExistsException(UserNotExistsException e){
        Map<String,Object> result = new HashMap<>(16);
        result.put("id",e.getUserId());
        result.put("message",e.getMessage());
        return result;
    }
}
